package com.guodi.map.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guodi.map.entity.MapBookMark;
import com.guodi.map.mapper.MapBookMarkMapper;
import com.guodi.map.service.IMapBookMarkService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @描述：书签表 服务类
 * @作者: 陈宏成
 * @日期: 2021-03-30
 */
@Service
public class MapBookMarkServiceImpl extends ServiceImpl<MapBookMarkMapper, MapBookMark> implements IMapBookMarkService {

    @Resource
    private MapBookMarkMapper mapBookMarkMapper;

    /**
     * @描述：根据用户id和书签名获取
     * @入参： bookMarkName 书签名
     * @入参： userId 用户id
     * @出参： MapBookMark
     * @作者: 陈宏成
     * @日期: 2021-03-30
     */
    public MapBookMark getByEqName(String bookMarkName, String userId){
        LambdaQueryWrapper<MapBookMark> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MapBookMark::getName,bookMarkName.trim());
        lambdaQueryWrapper.eq(MapBookMark::getCreator,userId.trim());
        MapBookMark bookMark = this.getOne(lambdaQueryWrapper,false);
        return bookMark;
    }

    /**
     * @描述：删除书签
     * @入参： ids 主键id列表
     * @入参： userId 用户id
     * @出参： MapBookMark 被删除了的记录
     * @作者: 陈宏成
     * @日期: 2021-03-30
     */
    @Transactional(rollbackFor = Exception.class)
    public List<MapBookMark> deleteByIds(List<String> ids, String userId){
        if (ids == null || ids.isEmpty()){
            return new LinkedList<>();
        }
        LambdaQueryWrapper<MapBookMark> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(MapBookMark::getCreator,userId);
        lambdaQueryWrapper.in(MapBookMark::getId,ids);
        List<MapBookMark> bookMarks = this.list(lambdaQueryWrapper);
        List<String> deleteIds = bookMarks.stream().map(MapBookMark::getId).collect(Collectors.toList());
        this.removeByIds(deleteIds);
        return bookMarks;
    }

}
